package com.customview.CircleProgress;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;


public class CircleIProgress extends View {

    RectF m;
    Paint mpaint;


    public CircleIProgress(Context context) {
        this(context, null);

    }

    public CircleIProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleIProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMea(widthMeasureSpec), getMea(heightMeasureSpec));
        init();

    }

    private void init() {
        int width = getMeasuredHeight();
        int height = getMeasuredHeight();
        m = new RectF(0, 0, width, height);
        mpaint = new Paint();
        mpaint.setColor(Color.GREEN);
        mpaint.setStyle(Paint.Style.FILL);
        mpaint.setAntiAlias(true);
        setBackgroundColor(Color.BLUE);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    public int getMea(int mMeasureSpec) {
        int height = 0;
        int specMode = MeasureSpec.getMode(mMeasureSpec);
        int specSize = MeasureSpec.getSize(mMeasureSpec);
        switch (specMode) {
            case MeasureSpec.EXACTLY:// 明确指定了
                height = getPaddingTop() + getPaddingBottom() + specSize;
                break;
            case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
                height = getPaddingTop() + getPaddingBottom() + 30;
                break;
            case MeasureSpec.UNSPECIFIED://
                height = getPaddingTop() + getPaddingBottom() + 30;
                break;
        }
        return height;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (m != null) {
            canvas.drawArc(m, 0f, 180f, true, mpaint);

        }
    }

}
